﻿<?php
	session_start();
	require_once('../config.php');
	require_once('../paging.php');
	
	if(!isset($_SESSION['logon']))
	{
		header("Location: ". $config_basedir. "/WKSCP/login.php?url=sp_CMS");
		exit();
	}

	require_once '../model.php';
	
	
	
	if (isset($_GET['action']) == true){
		
		if (isset($_GET['id']) == true){
			$error = 0;
			if (is_numeric($_GET['id']) == false){
				$error = 1;
			}
			
			if ($error == 1){
				header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php");		
			}
			else{
				$id_sp = $_GET['id'];
			}
		}
	
		$action = $_GET['action'];
		
		if($action == 'LIST'){
			$numSp = GetNumSp();
			$rowPerPage = 5;
			
			if(isset($_GET['page']))
				$curPage = $_GET['page'];
			else
				$curPage = 0;
				
			// echo $curPage, '--', $rowPerPage; die(); 	
			$sp = GetSp2($curPage, $rowPerPage);
			//$sp = GetSp();
			//Xu ly
			require_once('template/sp/sp_view_CMS.phtml');
			exit();
		}
		
		if($action == 'ADD'){
			if(!isset($_POST['submit'])){
				$loai = GetLoai();
				$hieu = GetHieu();
				require_once('template/sp/sp_add_CMS.phtml');
				exit();
			}
			else{
				if($_POST['ten_sp'] != '')
				{
					//var_dump ($_FILES); die();
					$img_url = Upload($_FILES, 'sp-', $config_basedir);
					if($img_url == null)
						$img_url = "../images/sp/btn-normal.png";
					InsertSp($_POST, $img_url);
					header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php?action=LIST");
				}
			}
		}
		
		if($action == 'EDIT'){
			if (!isset($id_sp))
				header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php");		
				
			if(!isset($_POST['submit'])){
				$loai = GetLoai();
				$nhom = GetNhom();
				$hieu = GetHieu();
				$sp = GetSpById($id_sp);
				require_once('template/sp/sp_edit_CMS.phtml');
				exit();
			}
			else{
				if($_POST['ten_sp'] != ''){
					$img_url = Upload($_FILES, 'sp-', $config_basedir);
					EditSp($id_sp, $_POST, $img_url);
					header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php?action=LIST");
				}
			}
		}
		
		if($action == 'DEL'){
			if (isset($id_sp))
				DeleteSp($id_sp);
				
			header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php");		
		}
	}
	else{
		header("Location: ". $config_basedir. "/WKSCP/sp_CMS.php?action=LIST");	
	}
function Upload($FILE, $fName, $dir)
{	
	if  (((	$FILE["file"]["type"] == "image/gif")
		|| ($FILE["file"]["type"] == "image/jpeg")
		|| ($FILE["file"]["type"] == "image/jpg")
		|| ($FILE["file"]["type"] == "image/png")
		))
	{
		if ($FILE["file"]["error"] > 0)
		{
			return null;
		}
		else
		{
			 move_uploaded_file($FILE["file"]["tmp_name"], "../images/sp/" . $fName. $FILE["file"]["name"]);
			//rename("upload/" . $FILE["file"]["name"], "../images/sp" .  "$fName-". $FILE["file"]["name"]);
			//echo "Stored in: " . "../images/sp/" . $FILE["file"]["name"];die();
			
			return $dir. "/images/sp". "/$fName". $FILE['file']['name'];
		}
	}
	else
	{
		return null;
	}
}
?>


